package com.ruoyi.house_sale.service.impl;

import java.util.List;

import com.ruoyi.house_sale.domain.HouseEvent;
import com.ruoyi.house_sale.domain.PaidScheme;
import com.ruoyi.house_sale.domain.WorkProcess;
import com.ruoyi.house_sale.mapper.HouseEventMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.house_sale.mapper.BuyHouseMapper;
import com.ruoyi.house_sale.domain.BuyHouse;
import com.ruoyi.house_sale.service.IBuyHouseService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 购买房产Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-08
 */
@Service
public class BuyHouseServiceImpl implements IBuyHouseService
{
    @Resource
    private BuyHouseMapper buyHouseMapper;
    @Resource
    private HouseEventMapper houseEventMapper;

    /**
     * 查询购买房产
     *
     * @param buyid 购买房产主键
     * @return 购买房产
     */
    @Override
    public BuyHouse selectBuyHouseByBuyid(Long buyid)
    {
        return buyHouseMapper.selectBuyHouseByBuyid(buyid);
    }

    /**
     * 查询购买房产列表
     *
     * @param buyHouse 购买房产
     * @return 购买房产
     */
    @Override
    public List<BuyHouse> selectBuyHouseList(BuyHouse buyHouse)
    {
        return buyHouseMapper.selectBuyHouseList(buyHouse);
    }

    /**
     * 新增购买房产
     *
     * @param buyHouse 购买房产
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBuyHouse(BuyHouse buyHouse)
    {
        // 新增购买房产
        int count = buyHouseMapper.insertBuyHouse(buyHouse);
        // 新增房产事件
        HouseEvent houseEvent = new HouseEvent();
        houseEvent.setHouseid(buyHouse.getHourseid());
        houseEvent.setEventtype("认购");
        houseEvent.setUserid(buyHouse.getUserid());
        houseEvent.setOprtime(buyHouse.getOprtime());
        houseEvent.setBuyid(buyHouse.getBuyid());
        houseEvent.setExpire("客户["+buyHouse.getCustname()+"]正式认购该房产");
        houseEventMapper.insertHouseEvent(houseEvent);
        // 新增购房流程
        buyHouseMapper.callProcBuyHouse(buyHouse);

        return count;
    }

    /**
     * 修改购买房产
     *
     * @param buyHouse 购买房产
     * @return 结果
     */
    @Override
    public int updateBuyHouse(BuyHouse buyHouse)
    {
        return buyHouseMapper.updateBuyHouse(buyHouse);
    }

    /**
     * 批量删除购买房产
     *
     * @param buyids 需要删除的购买房产主键
     * @return 结果
     */
    @Override
    public int deleteBuyHouseByBuyids(Long[] buyids)
    {
        return buyHouseMapper.deleteBuyHouseByBuyids(buyids);
    }

    /**
     * 删除购买房产信息
     *
     * @param buyid 购买房产主键
     * @return 结果
     */
    @Override
    public int deleteBuyHouseByBuyid(Long buyid)
    {
        return buyHouseMapper.deleteBuyHouseByBuyid(buyid);
    }

    /**
     * 退房
     * @param houseEvent
     */
    @Override
    public void checkout(HouseEvent houseEvent) {
        buyHouseMapper.callProcCheckout(houseEvent);
    }

    /**
     * 换房
     * @param buyHouse
     */
    @Override
    public int change(BuyHouse buyHouse) {
        // 换房操作
        buyHouseMapper.callProcChangeHouse(buyHouse);
        // 更新买房表字段
        return buyHouseMapper.updateBuyHouse(buyHouse);
    }

    /**
     * 查询付款方案列表
     *
     * @param paidScheme 付款方案
     * @return 付款方案集合
     */
    @Override
    public List<PaidScheme> selectPaidSchemeList(PaidScheme paidScheme) {
        return buyHouseMapper.selectPaidSchemeList(paidScheme);
    }

    @Override
    public Long selecthouserid(Long buyid) {
        return buyHouseMapper.selecthouserid(buyid);
    }

    @Override
    public List<BuyHouse> findAllhouseList(BuyHouse buyHouse) {
        return buyHouseMapper.findAllhouseList(buyHouse);
    }
}
